﻿using System;
using System.Collections.Generic; 
using System.Text;
using Runda.EShop.Common.Utility;
using Runda.EShop.Common.Entity;
using System.Data;
using Runda.EShop.Dal;
using System.Data.Common;

namespace Runda.EShop.Bll
{
    public class CustomerBll:BllBase
    {
        public void SaveCustomerCate(Guid CustomerID, List<RelationCustomerProductCateEntity> list)
        {
            base.SafeDbExecute(delegate(IDbTransaction tran)
            {
                RelationCustomerProductCateDal dal = new RelationCustomerProductCateDal(tran as DbTransaction);
                    dal.DeleteByCustomer(CustomerID);
                    dal.BulkInsert(list);
                }
            ); 
        }

        public void SaveCustomerCate(Guid CustomerID, int CateType, List<RelationCustomerProductCateEntity> list)
        {
            base.SafeDbExecute(delegate(IDbTransaction tran)
            {
                RelationCustomerProductCateDal dal = new RelationCustomerProductCateDal(tran as DbTransaction);
                dal.DeleteByCustomer(CustomerID,CateType);
                dal.BulkInsert(list);
            }
            );
        }

        public List<BasProductCateEntity> BuildProductCategory(Guid CustomerID)
        {
            BasProductCateDal dal = new BasProductCateDal();
            DataTable dt = dal.BuildProductCategory(CustomerID);

            return this.BuildEntityListFromDataTable<BasProductCateEntity>(dt);
        }

        public List<BasProductCateEntity> BuildProductCategory(Guid CustomerID,int CateType)
        {
            BasProductCateDal dal = new BasProductCateDal();
            DataTable dt = dal.BuildProductCategory(CustomerID,CateType);

            return this.BuildEntityListFromDataTable<BasProductCateEntity>(dt);
        }

        public List<BasProductCateEntity> GetProductCateListByCustomerID(Guid CustomerID)
        {
            BasProductCateDal dal = new BasProductCateDal();
            return dal.GetProductCateListByCustomerID(CustomerID);
        }

        public List<BasProductCateEntity> GetProductCateListByCustomerID(Guid CustomerID,int CateType)
        {
            BasProductCateDal dal = new BasProductCateDal();
            return dal.GetProductCateListByCustomerID(CustomerID,CateType);
        }

        public DataTable GetMyShopcart(Guid CustomerID)
        {
            BizShopCartDal dal = new BizShopCartDal();
            return dal.GetShopcartByCustomerID(CustomerID);
        }

        /// <summary>
        /// 修改定单数量
        /// </summary>
        /// <param name="list"></param>
        public void ModifyShopcart(List<BizShopCartEntity> list)
        {
            BizShopCartDal dal = new BizShopCartDal();
            foreach (BizShopCartEntity entity in list)
            {
                dal.ModifyShopcart(entity);
            }
        }

        public void SetCustomerDefaultAddress(BizEntAddrEntity entity)
        {
            using (System.Transactions.TransactionScope tran = new System.Transactions.TransactionScope())
            {
                BizEntAddrDal dal = new BizEntAddrDal();
                dal.UpdateAddressNonDefault(entity.EntID);

                dal.UpdateAddressDefault(entity.ID, "Y");

                tran.Complete();
            }
        }

        public void UpdateCustomerPassword(Guid CustomerID, string Password)
        {
            BizCustomerDal dal = new BizCustomerDal();
            dal.UpdateCustomerPassword(CustomerID, Password);
        }

        public void AddCustomerFavorite(BizCustomerFavoriteEntity entity)
        {
            BizCustomerFavoriteDal dal = new BizCustomerFavoriteDal();
            dal.AddCustomerFavorite(entity);
        }

        public DataTable GetCustomerFavorite(List<SqlDbParameter> parms, string OrderBy, int PageSize, int PageIndex, out int RecordCount)
        {
            BizCustomerFavoriteDal dal = new BizCustomerFavoriteDal();
            return dal.GetCustomerFavorite(parms, OrderBy, PageSize, PageIndex, out RecordCount);
        }
    }
}
